from datetime import datetime
import random
import string

from common.setting import ensure_path_sep
from util.mysqlUtils.db_pool import Database
from util.otherutils.functions_control import randomint


def generate_random_string(length):
    """生成指定长度的随机字符串"""
    letters = string.ascii_lowercase + string.digits
    return ''.join(random.choice(letters) for i in range(length))


def generate_random_ip():
    """生成随机的 IP 地址"""
    return '.'.join(str(random.randint(0, 255)) for _ in range(4))


def generate_random_mac():
    """生成随机的 MAC 地址"""
    return ':'.join('{:02x}'.format(random.randint(0, 255)) for _ in range(6))


d_cginstance_db = Database(ensure_path_sep("\\config\\mysql_config.yaml"), "sit_union", "d_cginstance")


class GenerageData:

    def generage_data(self):
        # 生成随机的时间
        # now = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
        # operation_id = f'ins-{generate_random_string(32)}'
        # sub_task_id = f'ins-{generate_random_string(32)}'
        # # 生成随机的 ID
        # g_instance_id = f'ins-{generate_random_string(32)}'
        # g_server_id = f'hyp-{generate_random_string(20)}'
        # # 随机生成其他字段的值
        # project_id = 0
        # status = 0
        # supplier = 1
        # supplier_instance_id = g_instance_id
        # supplier_server_id = g_server_id
        # supplier_order_id = str(random.randint(10000, 99999))
        # supplier_business_status = 'InstanceNormal'
        # ins_state = 'Running'
        # task_state = 'None'
        # reboot_at = None
        # multi_count = 0
        # remark = None
        # current_sub_task_id = ''
        # multi_open_arch = 0
        get_instance_info = f"""
        select g_server_id,g_instance_id from d_cginstance.t_instance where system_image_id = "os-s590m62vrsvyaytvvi27145lvhii"
        """

        res = d_cginstance_db.fetch_all_tuple(get_instance_info,())
        for i in res:
            now = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
            operation_id = f'{generate_random_string(32)}'
            sub_task_id = f'{generate_random_string(32)}'
            sql_instance = f"""
               INSERT INTO d_cginstance.t_task_operation (
                operation_type,
                operation_id,
                sub_task_id,
                g_server_id,
                g_instance_id,
                supplier_task_id,
                state,
                extra_info,
                retries,
                start_time,
                end_time
                ) VALUES (
                'ServiceUpgrade',
                %s,
                %s,
                %s,
                %s,
                %s,
                'NotStarted',
                NULL,
                0,
                NULL,
                NULL
                );
                """
            rows1 = d_cginstance_db.execute(sql_instance, (operation_id,sub_task_id,i[0],i[1],sub_task_id))
    def generate_many_data(self,num):
        for i in range(num):
            self.generage_data()





if __name__ == '__main__':

    GenerageData().generage_data()
